<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.38" />
    <meta name="theme" content="VuePress Theme Hope" />
    <meta property="og:url" content="https://github.com.io/article/javascript/js2.html"><meta property="og:site_name" content="Hangover"><meta property="og:title" content="数组常用方法"><meta property="og:description" content="数组的常用方法"><meta property="og:type" content="article"><meta property="og:locale" content="zh-CN"><meta property="article:tag" content="方法"><meta property="article:published_time" content="2022-04-22T00:00:00.000Z"><link rel="stylesheet" href="//at.alicdn.com/t/font_2410206_mfj6e1vbwo.css"><link rel="stylesheet" href="//at.alicdn.com/t/font_3325523_eud95fd11si.css"><link rel="stylesheet" href="//at.alicdn.com/t/font_3354094_xuy6jxxju39.css"><link rel="icon" href="/favicon.ico"><title>数组常用方法 | Hangover</title><meta name="description" content="数组的常用方法">
    <style>
      :root {
        --bg-color: #fff;
      }

      html[data-theme="dark"] {
        --bg-color: #1d2025;
      }

      html,
      body {
        background-color: var(--bg-color);
      }
    </style>
    <script>
      const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
      const systemDarkMode =
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches;

      if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
        document.querySelector("html").setAttribute("data-theme", "dark");
      }
    </script>
    <link rel="stylesheet" href="/assets/style.76e0a9d2.css">
    <link rel="modulepreload" href="/assets/app.0d0ab3c5.js"><link rel="modulepreload" href="/assets/js2.html.19d11580.js"><link rel="modulepreload" href="/assets/js2.html.d6254eea.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper.21dcd24c.js">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="skip-link sr-only">Skip to content</a><!--]--><div class="theme-container has-toc sidebar-open"><!--[--><header class="navbar"><button class="toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><a href="/" class="home-link"><img class="logo" src="/logo.png" alt="Hangover"><!----><span class="site-name hide-in-pad">Hangover</span><!--[--><!----><!--]--></a><nav class="nav-links" style=""><div class="nav-item hide-in-mobile"><a href="/" class="nav-link" aria-label="首页"><i class="icon iconfont icon-home"></i>首页<!----></a></div><div class="nav-item hide-in-mobile"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="文章"><span class="title"><i class="icon iconfont icon-flower"></i>文章</span><span class="arrow"></span><ul class="nav-dropdown"><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>Tampermonkey</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/article/tampermonkey/tm1.html" class="nav-link" aria-label="油猴 API"><i class="icon iconfont icon-a-apichengxushezhichiluncanshu"></i>油猴 API<!----></a></li><li class="dropdown-subitem"><a href="/article/tampermonkey/tm2.html" class="nav-link" aria-label="免费翻译API"><i class="icon iconfont icon-fanyi"></i>免费翻译API<!----></a></li><li class="dropdown-subitem"><a href="/article/tampermonkey/tm3.html" class="nav-link" aria-label="百度翻译API"><i class="icon iconfont icon-Group"></i>百度翻译API<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>JavaScript</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/article/javascript/js1.html" class="nav-link" aria-label="类型转换机制"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>类型转换机制<!----></a></li><li class="dropdown-subitem"><a aria-current="page" href="/article/javascript/js2.html" class="router-link-active router-link-exact-active nav-link active" aria-label="数组常用方法"><i class="icon iconfont icon-random1"></i>数组常用方法<!----></a></li><li class="dropdown-subitem"><a href="/article/javascript/js3.html" class="nav-link" aria-label="字符串常用方法"><i class="icon iconfont icon-string"></i>字符串常用方法<!----></a></li><li class="dropdown-subitem"><a href="/article/javascript/js4.html" class="nav-link" aria-label="防抖与节流"><i class="icon iconfont icon-fangdou"></i>防抖与节流<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>CSS</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/article/css/css1.html" class="nav-link" aria-label="响应式设计"><i class="icon iconfont icon-xiangyingshi"></i>响应式设计<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css2.html" class="nav-link" aria-label="flex弹性布局"><i class="icon iconfont icon-flex"></i>flex弹性布局<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css3.html" class="nav-link" aria-label="grid网格布局"><i class="icon iconfont icon-grid"></i>grid网格布局<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css4.html" class="nav-link" aria-label="视差滚动效果"><i class="icon iconfont icon-shichagundong"></i>视差滚动效果<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css5.html" class="nav-link" aria-label="文本溢出省略"><i class="icon iconfont icon-wenbenyichuzhanshi"></i>文本溢出省略<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css6.html" class="nav-link" aria-label="css画三角形"><i class="icon iconfont icon-sanjiaoxing"></i>css画三角形<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css7.html" class="nav-link" aria-label="css实现动画"><i class="icon iconfont icon-style"></i>css实现动画<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css8.html" class="nav-link" aria-label="隐藏页面元素"><i class="icon iconfont icon-yincangbukejian"></i>隐藏页面元素<!----></a></li></ul></li></ul></button></div></div><div class="nav-item hide-in-mobile"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="代码片段"><span class="title"><i class="icon iconfont icon-template"></i>代码片段</span><span class="arrow"></span><ul class="nav-dropdown"><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>数组方法</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/array/array1.html" class="nav-link" aria-label="数组合并"><i class="icon iconfont icon-shujujihebing-xuanzhong"></i>数组合并<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array2.html" class="nav-link" aria-label="最大/最小值"><i class="icon iconfont icon-zuidazhizuixiaozhi"></i>最大/最小值<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array3.html" class="nav-link" aria-label="Set 对象作用"><i class="icon iconfont icon-quweiyizhi"></i>Set 对象作用<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array4.html" class="nav-link" aria-label="过滤虚假值"><i class="icon iconfont icon-guolvtiaojian"></i>过滤虚假值<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array5.html" class="nav-link" aria-label="数组洗牌"><i class="icon iconfont icon-suijishushengcheng-xuanzhong"></i>数组洗牌<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array6.html" class="nav-link" aria-label="删除重复元素"><i class="icon iconfont icon-shujuguolv"></i>删除重复元素<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array7.html" class="nav-link" aria-label="数组比较"><i class="icon iconfont icon-icon-test"></i>数组比较<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>字符串操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/string/string1.html" class="nav-link" aria-label="字符串和数字互相转换"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>字符串和数字互相转换<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string2.html" class="nav-link" aria-label="字符串首字母大写"><i class="icon iconfont icon-daxie"></i>字符串首字母大写<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string3.html" class="nav-link" aria-label="翻转字符串"><i class="icon iconfont icon-fanzhuan"></i>翻转字符串<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string4.html" class="nav-link" aria-label="随机字符串"><i class="icon iconfont icon-random"></i>随机字符串<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string5.html" class="nav-link" aria-label="截断字符串"><i class="icon iconfont icon-duankai"></i>截断字符串<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>数字操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/number/number1.html" class="nav-link" aria-label="判断奇数、偶数"><i class="icon iconfont icon-panduan"></i>判断奇数、偶数<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number2.html" class="nav-link" aria-label="计算平均值"><i class="icon iconfont icon-col-avg"></i>计算平均值<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number3.html" class="nav-link" aria-label="随机整数"><i class="icon iconfont icon-suijishushengcheng-xuanzhong"></i>随机整数<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number4.html" class="nav-link" aria-label="四舍五入"><i class="icon iconfont icon-sishewuru"></i>四舍五入<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number5.html" class="nav-link" aria-label="去除数字之外的所有字符"><i class="icon iconfont icon-guolv"></i>去除数字之外的所有字符<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>时间操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/time/time1.html" class="nav-link" aria-label="当前时间与倒计时"><i class="icon iconfont icon-daojishi"></i>当前时间与倒计时<!----></a></li><li class="dropdown-subitem"><a href="/template/time/time2.html" class="nav-link" aria-label="提取时间"><i class="icon iconfont icon-shijian"></i>提取时间<!----></a></li><li class="dropdown-subitem"><a href="/template/time/time3.html" class="nav-link" aria-label="时间格式化"><i class="icon iconfont icon-shijianzhuanhuangeshi"></i>时间格式化<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>Promise</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/promise/promise1.html" class="nav-link" aria-label="红绿灯交替"><i class="icon iconfont icon-honglvdeng"></i>红绿灯交替<!----></a></li><li class="dropdown-subitem"><a href="/template/promise/promise2.html" class="nav-link" aria-label="异步加载图片"><i class="icon iconfont icon-loading"></i>异步加载图片<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>JavaScript</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/javascript/js1.html" class="nav-link" aria-label="获取变量类型"><i class="icon iconfont icon-leixing"></i>获取变量类型<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js2.html" class="nav-link" aria-label="华氏度和摄氏度"><i class="icon iconfont icon-wendu"></i>华氏度和摄氏度<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js3.html" class="nav-link" aria-label="动态引入js"><i class="icon iconfont icon-java-script"></i>动态引入js<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js4.html" class="nav-link" aria-label="判断类型"><i class="icon iconfont icon-he_76xuexileixingguanli"></i>判断类型<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>CSS</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/css/css1.html" class="nav-link" aria-label="loading动画"><i class="icon iconfont icon-loading"></i>loading动画<!----></a></li></ul></li></ul></button></div></div><div class="nav-item hide-in-mobile"><a href="/home.html" class="nav-link" aria-label="项目收藏"><i class="icon iconfont icon-module"></i>项目收藏<!----></a></div><div class="nav-item hide-in-mobile"><a href="/bbs.html" class="nav-link" aria-label="留言板"><i class="icon iconfont icon-mark"></i>留言板<!----></a></div></nav><div class="nav-actions-wrapper"><!--[--><!----><!--]--><div class="nav-item"><!----></div><div class="nav-item"><a class="repo-link" href="https://github.com/zuifanzhangsan" target="_blank" rel="noopener noreferrer"><svg xmlns="http://www.w3.org/2000/svg" class="icon github-icon" viewbox="0 0 1024 1024" aria-labelledby="github" style="width:1.25rem;height:1.25rem;vertical-align:middle;"><title id="github" lang="en">github icon</title><g fill="currentColor"><path d="M511.957 21.333C241.024 21.333 21.333 240.981 21.333 512c0 216.832 140.544 400.725 335.574 465.664 24.49 4.395 32.256-10.07 32.256-23.083 0-11.69.256-44.245 0-85.205-136.448 29.61-164.736-64.64-164.736-64.64-22.315-56.704-54.4-71.765-54.4-71.765-44.587-30.464 3.285-29.824 3.285-29.824 49.195 3.413 75.179 50.517 75.179 50.517 43.776 75.008 114.816 53.333 142.762 40.79 4.523-31.66 17.152-53.377 31.19-65.537-108.971-12.458-223.488-54.485-223.488-242.602 0-53.547 19.114-97.323 50.517-131.67-5.035-12.33-21.93-62.293 4.779-129.834 0 0 41.258-13.184 134.912 50.346a469.803 469.803 0 0 1 122.88-16.554c41.642.213 83.626 5.632 122.88 16.554 93.653-63.488 134.784-50.346 134.784-50.346 26.752 67.541 9.898 117.504 4.864 129.834 31.402 34.347 50.474 78.123 50.474 131.67 0 188.586-114.73 230.016-224.042 242.09 17.578 15.232 33.578 44.672 33.578 90.454v135.85c0 13.142 7.936 27.606 32.854 22.87C862.25 912.597 1002.667 728.747 1002.667 512c0-271.019-219.648-490.667-490.71-490.667z"></path></g></svg></a></div><div class="nav-item hide-in-mobile"><button class="outlook-button" tabindex="-1" ariahidden="true"><svg xmlns="http://www.w3.org/2000/svg" class="icon outlook-icon" viewbox="0 0 1024 1024" aria-labelledby="outlook"><title id="outlook" lang="en">outlook icon</title><g fill="currentColor"><path d="M224 800c0 9.6 3.2 44.8 6.4 54.4 6.4 48-48 76.8-48 76.8s80 41.6 147.2 0 134.4-134.4 38.4-195.2c-22.4-12.8-41.6-19.2-57.6-19.2C259.2 716.8 227.2 761.6 224 800zM560 675.2l-32 51.2c-51.2 51.2-83.2 32-83.2 32 25.6 67.2 0 112-12.8 128 25.6 6.4 51.2 9.6 80 9.6 54.4 0 102.4-9.6 150.4-32l0 0c3.2 0 3.2-3.2 3.2-3.2 22.4-16 12.8-35.2 6.4-44.8-9.6-12.8-12.8-25.6-12.8-41.6 0-54.4 60.8-99.2 137.6-99.2 6.4 0 12.8 0 22.4 0 12.8 0 38.4 9.6 48-25.6 0-3.2 0-3.2 3.2-6.4 0-3.2 3.2-6.4 3.2-6.4 6.4-16 6.4-16 6.4-19.2 9.6-35.2 16-73.6 16-115.2 0-105.6-41.6-198.4-108.8-268.8C704 396.8 560 675.2 560 675.2zM224 419.2c0-28.8 22.4-51.2 51.2-51.2 28.8 0 51.2 22.4 51.2 51.2 0 28.8-22.4 51.2-51.2 51.2C246.4 470.4 224 448 224 419.2zM320 284.8c0-22.4 19.2-41.6 41.6-41.6 22.4 0 41.6 19.2 41.6 41.6 0 22.4-19.2 41.6-41.6 41.6C339.2 326.4 320 307.2 320 284.8zM457.6 208c0-12.8 12.8-25.6 25.6-25.6 12.8 0 25.6 12.8 25.6 25.6 0 12.8-12.8 25.6-25.6 25.6C470.4 233.6 457.6 220.8 457.6 208zM128 505.6C128 592 153.6 672 201.6 736c28.8-60.8 112-60.8 124.8-60.8-16-51.2 16-99.2 16-99.2l316.8-422.4c-48-19.2-99.2-32-150.4-32C297.6 118.4 128 291.2 128 505.6zM764.8 86.4c-22.4 19.2-390.4 518.4-390.4 518.4-22.4 28.8-12.8 76.8 22.4 99.2l9.6 6.4c35.2 22.4 80 12.8 99.2-25.6 0 0 6.4-12.8 9.6-19.2 54.4-105.6 275.2-524.8 288-553.6 6.4-19.2-3.2-32-19.2-32C777.6 76.8 771.2 80 764.8 86.4z"></path></g></svg><div class="outlook-dropdown"><!----></div></button></div><div id="docsearch-container"></div><button class="toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span class="button-container"><span class="button-top"></span><span class="button-middle"></span><span class="button-bottom"></span></span></button><!--[--><!----><!--]--></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow left"></span></div><aside class="sidebar"><!--[--><!----><!--]--><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><p class="sidebar-heading active"><i class="icon iconfont icon-flower"></i><span class="title">文章</span><!----></p><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-Tampermonkey"></i><span class="title">Tampermonkey</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable active"><i class="icon iconfont icon-javascript"></i><span class="title">JavaScript</span><span class="arrow down"></span></button><ul class="sidebar-links"><li><!--[--><a href="/article/javascript/js1.html" class="nav-link sidebar-link sidebar-page" aria-label="类型转换机制"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>类型转换机制<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li><li><!--[--><a aria-current="page" href="/article/javascript/js2.html" class="router-link-active router-link-exact-active nav-link active sidebar-link sidebar-page active" aria-label="数组常用方法"><i class="icon iconfont icon-random1"></i>数组常用方法<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#一、操作方法" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="一、操作方法"><!---->一、操作方法<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#增" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="增"><!---->增<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#push" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="push()"><!---->push()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#unshift" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="unshift()"><!---->unshift()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#splice" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="splice()"><!---->splice()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#concat" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="concat()"><!---->concat()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#删" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="删"><!---->删<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#pop" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="pop()"><!---->pop()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#shift" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="shift()"><!---->shift()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#splice-1" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="splice()"><!---->splice()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#slice" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="slice()"><!---->slice()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#改" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="改"><!---->改<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#splice-2" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="splice()"><!---->splice()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#查" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="查"><!---->查<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#indexof" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="indexOf()"><!---->indexOf()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#includes" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="includes()"><!---->includes()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#find" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="find()"><!---->find()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#二、排序方法" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="二、排序方法"><!---->二、排序方法<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#reverse" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="reverse()"><!---->reverse()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#sort" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="sort()"><!---->sort()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#三、转换方法" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="三、转换方法"><!---->三、转换方法<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#join" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="join()"><!---->join()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#四、迭代方法" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="四、迭代方法"><!---->四、迭代方法<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#some" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="some()"><!---->some()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#every" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="every()"><!---->every()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#foreach" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="forEach()"><!---->forEach()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#filter" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="filter()"><!---->filter()<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/javascript/js2.html#map" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="map()"><!---->map()<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li></ul><!--]--></li><li><!--[--><a href="/article/javascript/js3.html" class="nav-link sidebar-link sidebar-page" aria-label="字符串常用方法"><i class="icon iconfont icon-string"></i>字符串常用方法<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li><li><!--[--><a href="/article/javascript/js4.html" class="nav-link sidebar-link sidebar-page" aria-label="防抖与节流"><i class="icon iconfont icon-fangdou"></i>防抖与节流<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li></ul></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-css"></i><span class="title">CSS</span><span class="arrow right"></span></button><!----></section><!--]--></li></ul></section><!--]--></li></ul><!--[--><!----><!--]--></aside><!--[--><main class="page" id="main-content"><!----><nav class="breadcrumb disable"></nav><div class="page-title"><h1><i class="icon iconfont icon-random1"></i>数组常用方法</h1><div class="page-info"><span class="author-info" aria-label="作者🖊" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon author-icon" viewbox="0 0 1024 1024" aria-labelledby="author"><title id="author" lang="en">author icon</title><g fill="currentColor"><path d="M649.6 633.6c86.4-48 147.2-144 147.2-249.6 0-160-128-288-288-288s-288 128-288 288c0 108.8 57.6 201.6 147.2 249.6-121.6 48-214.4 153.6-240 288-3.2 9.6 0 19.2 6.4 25.6 3.2 9.6 12.8 12.8 22.4 12.8h704c9.6 0 19.2-3.2 25.6-12.8 6.4-6.4 9.6-16 6.4-25.6-25.6-134.4-121.6-240-243.2-288z"></path></g></svg><span><span class="author-item">Hangover</span></span><span property="author" content="Hangover"></span></span><!----><span class="date-info" aria-label="写作日期📅" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewbox="0 0 1024 1024" aria-labelledby="calendar"><title id="calendar" lang="en">calendar icon</title><g fill="currentColor"><path d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z"></path></g></svg><span>2022年4月22日</span><meta property="datePublished" content="2022-04-22T00:00:00.000Z"></span><span class="category-info" aria-label="分类🌈" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon category-icon" viewbox="0 0 1024 1024" aria-labelledby="category"><title id="category" lang="en">category icon</title><g fill="currentColor"><path d="M148.41 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H148.41c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.311-40.31zM147.556 553.478H429.73c22.263 0 40.311 18.048 40.311 40.31v282.176c0 22.263-18.048 40.312-40.31 40.312H147.555c-22.263 0-40.311-18.049-40.311-40.312V593.79c0-22.263 18.048-40.311 40.31-40.311zM593.927 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H593.927c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.31-40.31zM730.22 920.502H623.926c-40.925 0-74.22-33.388-74.22-74.425V623.992c0-41.038 33.387-74.424 74.425-74.424h222.085c41.038 0 74.424 33.226 74.424 74.067v114.233c0 10.244-8.304 18.548-18.547 18.548s-18.548-8.304-18.548-18.548V623.635c0-20.388-16.746-36.974-37.33-36.974H624.13c-20.585 0-37.331 16.747-37.331 37.33v222.086c0 20.585 16.654 37.331 37.126 37.331H730.22c10.243 0 18.547 8.304 18.547 18.547 0 10.244-8.304 18.547-18.547 18.547z"></path></g></svg><ul class="categories-wrapper"><li class="category category5 clickable" role="navigation">数组</li><meta property="articleSection" content="数组"></ul></span><span aria-label="标签🏷" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon tag-icon" viewbox="0 0 1024 1024" aria-labelledby="tag"><title id="tag" lang="en">tag icon</title><g fill="currentColor"><path d="M939.902 458.563L910.17 144.567c-1.507-16.272-14.465-29.13-30.737-30.737L565.438 84.098h-.402c-3.215 0-5.726 1.005-7.634 2.913l-470.39 470.39a10.004 10.004 0 000 14.164l365.423 365.424c1.909 1.908 4.42 2.913 7.132 2.913s5.223-1.005 7.132-2.913l470.39-470.39c2.01-2.11 3.014-5.023 2.813-8.036zm-240.067-72.121c-35.458 0-64.286-28.828-64.286-64.286s28.828-64.285 64.286-64.285 64.286 28.828 64.286 64.285-28.829 64.286-64.286 64.286z"></path></g></svg><ul class="tags-wrapper"><li class="tag tag7 clickable" role="navigation">方法</li></ul><meta property="keywords" content="方法"></span><span class="reading-time-info" aria-label="阅读时间⌛" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon timer-icon" viewbox="0 0 1024 1024" aria-labelledby="timer"><title id="timer" lang="en">timer icon</title><g fill="currentColor"><path d="M799.387 122.15c4.402-2.978 7.38-7.897 7.38-13.463v-1.165c0-8.933-7.38-16.312-16.312-16.312H256.33c-8.933 0-16.311 7.38-16.311 16.312v1.165c0 5.825 2.977 10.874 7.637 13.592 4.143 194.44 97.22 354.963 220.201 392.763-122.204 37.542-214.893 196.511-220.2 389.397-4.661 5.049-7.638 11.651-7.638 19.03v5.825h566.49v-5.825c0-7.379-2.849-13.981-7.509-18.9-5.049-193.016-97.867-351.985-220.2-389.527 123.24-37.67 216.446-198.453 220.588-392.892zM531.16 450.445v352.632c117.674 1.553 211.787 40.778 211.787 88.676H304.097c0-48.286 95.149-87.382 213.728-88.676V450.445c-93.077-3.107-167.901-81.297-167.901-177.093 0-8.803 6.99-15.793 15.793-15.793 8.803 0 15.794 6.99 15.794 15.793 0 80.261 63.69 145.635 142.01 145.635s142.011-65.374 142.011-145.635c0-8.803 6.99-15.793 15.794-15.793s15.793 6.99 15.793 15.793c0 95.019-73.789 172.82-165.96 177.093z"></path></g></svg><span>大约 4 分钟</span><meta property="timeRequired" content="PT4M"></span></div><hr></div><div class="toc-place-holder"><aside id="toc-list"><div class="toc-header">此页内容</div><div class="toc-wrapper"><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#一、操作方法" class="router-link-active router-link-exact-active toc-link level2">一、操作方法</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#增" class="router-link-active router-link-exact-active toc-link level2">增</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#push" class="router-link-active router-link-exact-active toc-link level3">push()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#unshift" class="router-link-active router-link-exact-active toc-link level3">unshift()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#splice" class="router-link-active router-link-exact-active toc-link level3">splice()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#concat" class="router-link-active router-link-exact-active toc-link level3">concat()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#删" class="router-link-active router-link-exact-active toc-link level2">删</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#pop" class="router-link-active router-link-exact-active toc-link level3">pop()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#shift" class="router-link-active router-link-exact-active toc-link level3">shift()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#splice-1" class="router-link-active router-link-exact-active toc-link level3">splice()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#slice" class="router-link-active router-link-exact-active toc-link level3">slice()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#改" class="router-link-active router-link-exact-active toc-link level2">改</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#splice-2" class="router-link-active router-link-exact-active toc-link level3">splice()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#查" class="router-link-active router-link-exact-active toc-link level2">查</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#indexof" class="router-link-active router-link-exact-active toc-link level3">indexOf()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#includes" class="router-link-active router-link-exact-active toc-link level3">includes()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#find" class="router-link-active router-link-exact-active toc-link level3">find()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#二、排序方法" class="router-link-active router-link-exact-active toc-link level2">二、排序方法</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#reverse" class="router-link-active router-link-exact-active toc-link level3">reverse()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#sort" class="router-link-active router-link-exact-active toc-link level3">sort()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#三、转换方法" class="router-link-active router-link-exact-active toc-link level2">三、转换方法</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#join" class="router-link-active router-link-exact-active toc-link level3">join()</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#四、迭代方法" class="router-link-active router-link-exact-active toc-link level2">四、迭代方法</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#some" class="router-link-active router-link-exact-active toc-link level3">some()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#every" class="router-link-active router-link-exact-active toc-link level3">every()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#foreach" class="router-link-active router-link-exact-active toc-link level3">forEach()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#filter" class="router-link-active router-link-exact-active toc-link level3">filter()</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/javascript/js2.html#map" class="router-link-active router-link-exact-active toc-link level3">map()</a></li><!----><!--]--></ul><!--]--></ul></div></aside></div><!----><div class="theme-hope-content"><!--[--><p><img src="/article/js/js2.png" alt="数组方法" loading="lazy"></p><h2 id="一、操作方法" tabindex="-1"><a class="header-anchor" href="#一、操作方法" aria-hidden="true">#</a> 一、操作方法</h2><p>数组基本操作可以归纳为 增、删、改、查，需要留意的是哪些方法会对原数组产生影响，哪些方法不会</p><p>下面对数组常用的操作方法做一个归纳</p><h2 id="增" tabindex="-1"><a class="header-anchor" href="#增" aria-hidden="true">#</a> 增</h2><p>下面前三种是对原数组产生影响的增添方法，第四种则不会对原数组产生影响</p><ul><li>push()</li><li>unshift()</li><li>splice()</li><li>concat()</li></ul><h3 id="push" tabindex="-1"><a class="header-anchor" href="#push" aria-hidden="true">#</a> push()</h3><p>push()方法接收任意数量的参数，并将它们添加到数组末尾，返回数组的最新长度</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 创建一个数组</span>
<span class="token keyword">let</span> count <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 推入两项</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span> <span class="token comment">// 2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="unshift" tabindex="-1"><a class="header-anchor" href="#unshift" aria-hidden="true">#</a> unshift()</h3><p>unshift()在数组开头添加任意多个值，然后返回新的数组长度</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建一个数组</span>
<span class="token keyword">let</span> count <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 从数组开头推入两项</span>
<span class="token function">alert</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="splice" tabindex="-1"><a class="header-anchor" href="#splice" aria-hidden="true">#</a> splice()</h3><p>传入三个参数，分别是开始位置、0（要删除的元素数量）、插入的元素，返回空数组</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> removed <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;yellow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;orange&quot;</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span> <span class="token comment">// red,yellow,orange,green,blue</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>removed<span class="token punctuation">)</span> <span class="token comment">// []</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="concat" tabindex="-1"><a class="header-anchor" href="#concat" aria-hidden="true">#</a> concat()</h3><p>首先会创建一个当前数组的副本，然后再把它的参数添加到副本末尾，最后返回这个新构建的数组，不会影响原始数组</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> colors2 <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">&quot;yellow&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">&quot;black&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;brown&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;red&quot;, &quot;green&quot;,&quot;blue&quot;]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;, &quot;black&quot;, &quot;brown&quot;]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="删" tabindex="-1"><a class="header-anchor" href="#删" aria-hidden="true">#</a> 删</h2><p>下面三种都会影响原数组，最后一项不影响原数组：</p><ul><li>pop()</li><li>shift()</li><li>splice()</li><li>slice()</li></ul><h3 id="pop" tabindex="-1"><a class="header-anchor" href="#pop" aria-hidden="true">#</a> pop()</h3><p>pop() 方法用于删除数组的最后一项，同时减少数组的 length 值，返回被删除的项</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">]</span>
<span class="token keyword">let</span> item <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 取得最后一项</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// green</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token comment">// 1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="shift" tabindex="-1"><a class="header-anchor" href="#shift" aria-hidden="true">#</a> shift()</h3><p>shift()方法用于删除数组的第一项，同时减少数组的 length 值，返回被删除的项</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">]</span>
<span class="token keyword">let</span> item <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 取得第一项</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment">// red</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token comment">// 1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="splice-1" tabindex="-1"><a class="header-anchor" href="#splice-1" aria-hidden="true">#</a> splice()</h3><p>传入两个参数，分别是开始位置，删除元素的数量，返回包含删除元素的数组</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> removed <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 删除第一项</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// green,blue</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>removed<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// red，只有一个元素的数组</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="slice" tabindex="-1"><a class="header-anchor" href="#slice" aria-hidden="true">#</a> slice()</h3><p>slice() 用于创建一个包含原有数组中一个或多个元素的新数组，不会影响原始数组</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;yellow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;purple&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> colors2 <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> colors3 <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span>   <span class="token comment">// red,green,blue,yellow,purple</span>
concole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// green,blue,yellow,purple</span>
concole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors3<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// green,blue,yellow</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="改" tabindex="-1"><a class="header-anchor" href="#改" aria-hidden="true">#</a> 改</h2><p>即修改原来数组的内容，常用splice</p><h3 id="splice-2" tabindex="-1"><a class="header-anchor" href="#splice-2" aria-hidden="true">#</a> splice()</h3><p>传入三个参数，分别是开始位置，要删除元素的数量，要插入的任意多个元素，返回删除元素的数组，对原数组产生影响</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> removed <span class="token operator">=</span> colors<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;purple&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 插入两个值，删除一个元素</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// red,red,purple,blue</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>removed<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// green，只有一个元素的数组</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="查" tabindex="-1"><a class="header-anchor" href="#查" aria-hidden="true">#</a> 查</h2><p>即查找元素，返回元素坐标或者元素值</p><ul><li>indexOf()</li><li>includes()</li><li>find()</li></ul><h3 id="indexof" tabindex="-1"><a class="header-anchor" href="#indexof" aria-hidden="true">#</a> indexOf()</h3><p>返回要查找的元素在数组中的位置，如果没找到则返回-1</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
numbers<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="includes" tabindex="-1"><a class="header-anchor" href="#includes" aria-hidden="true">#</a> includes()</h3><p>返回要查找的元素在数组中的位置，找到返回true，否则false</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
numbers<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="find" tabindex="-1"><a class="header-anchor" href="#find" aria-hidden="true">#</a> find()</h3><p>返回第一个匹配的元素</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> people <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;Matt&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">27</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">29</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
people<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">element<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> element<span class="token punctuation">.</span>age <span class="token operator">&lt;</span> <span class="token number">28</span><span class="token punctuation">)</span> <span class="token comment">// // {name: &quot;Matt&quot;, age: 27}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="二、排序方法" tabindex="-1"><a class="header-anchor" href="#二、排序方法" aria-hidden="true">#</a> 二、排序方法</h2><p>数组有两个方法可以用来对元素重新排序：</p><ul><li>reverse()</li><li>sort()</li></ul><h3 id="reverse" tabindex="-1"><a class="header-anchor" href="#reverse" aria-hidden="true">#</a> reverse()</h3><p>顾名思义，将数组元素方向排列</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> values <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
values<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>values<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 5,4,3,2,1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="sort" tabindex="-1"><a class="header-anchor" href="#sort" aria-hidden="true">#</a> sort()</h3><p>sort()方法接受一个比较函数，用于判断哪个值应该排在前面</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token parameter">value1<span class="token punctuation">,</span> value2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>value1 <span class="token operator">&lt;</span> value2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>value1 <span class="token operator">&gt;</span> value2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">let</span> values <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
values<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>compare<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>values<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0,1,5,10,15</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="三、转换方法" tabindex="-1"><a class="header-anchor" href="#三、转换方法" aria-hidden="true">#</a> 三、转换方法</h2><p>常见的转换方法有：</p><h3 id="join" tabindex="-1"><a class="header-anchor" href="#join" aria-hidden="true">#</a> join()</h3><p>join() 方法接收一个参数，即字符串分隔符，返回包含所有项的字符串</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>colors<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;,&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// red,green,blue</span>
<span class="token function">alert</span><span class="token punctuation">(</span>colors<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;||&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// red||green||blue</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="四、迭代方法" tabindex="-1"><a class="header-anchor" href="#四、迭代方法" aria-hidden="true">#</a> 四、迭代方法</h2><p>常用来迭代数组的方法（都不改变原数组）有如下：</p><ul><li>some()</li><li>every()</li><li>forEach()</li><li>filter()</li><li>map()</li></ul><h3 id="some" tabindex="-1"><a class="header-anchor" href="#some" aria-hidden="true">#</a> some()</h3><p>对数组每一项都运行传入的函数，如果有一项函数返回 true ，则这个方法返回 true</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> someResult <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>someResult<span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="every" tabindex="-1"><a class="header-anchor" href="#every" aria-hidden="true">#</a> every()</h3><p>对数组每一项都运行传入的函数，如果对每一项函数都返回 true ，则这个方法返回 true</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> everyResult <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>everyResult<span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="foreach" tabindex="-1"><a class="header-anchor" href="#foreach" aria-hidden="true">#</a> forEach()</h3><p>对数组每一项都运行传入的函数，没有返回值</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
numbers<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 执行某些操作</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="filter" tabindex="-1"><a class="header-anchor" href="#filter" aria-hidden="true">#</a> filter()</h3><p>对数组每一项都运行传入的函数，函数返回 true 的项会组成数组之后返回</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> filterResult <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>filterResult<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3,4,5,4,3</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="map" tabindex="-1"><a class="header-anchor" href="#map" aria-hidden="true">#</a> map()</h3><p>对数组每一项都运行传入的函数，返回由每次函数调用的结果构成的数组</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> mapResult <span class="token operator">=</span> numbers<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> item <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>mapResult<span class="token punctuation">)</span> <span class="token comment">// 2,4,6,8,10,8,6,4,2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><!--]--></div><!----><footer class="page-meta"><!----><!----><!----></footer><nav class="page-nav"><a href="/article/javascript/js1.html" class="nav-link prev" aria-label="类型转换机制"><div class="hint"><span class="arrow left"></span>上一页</div><div class="link"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>类型转换机制</div></a><a href="/article/javascript/js3.html" class="nav-link next" aria-label="字符串常用方法"><div class="hint">下一页<span class="arrow right"></span></div><div class="link">字符串常用方法<i class="icon iconfont icon-string"></i></div></a></nav><div class="giscus-wrapper input-top" style="display:block;"><giscus-widget repo="zuifanzhangsan/giscus-blog" repoId="R_kgDOHOyKyA" category="Announcements" categoryId="DIC_kwDOHOyKyM4COw_c" lang="zh-CN" theme="light" mapping="pathname" inputPosition="top" reactionsEnabled="1" emitMetadata="0"></giscus-widget></div><!----></main><!--]--><footer class="footer-wrapper"><div class="footer">鸟无声兮山寂寂，夜正长兮风淅淅。 ──李华《吊古战场文》</div><div class="copyright">Copyright © 2023 Hangover</div></footer></div><!--]--><!----><!--]--></div>
    <script type="module" src="/assets/app.0d0ab3c5.js" defer></script>
  </body>
</html>
